package libT
{
	import flash.display.Graphics;
	import flash.display.MovieClip;
	import flash.media.Microphone;
	/**
	 * Provides a rectangular fill bar for things like HP Bars, Loading Bars, etc.
	 * @author truefire
	 */
	public class FillBar extends MovieClip 
	{
		
		private var background:MovieClip;
		private var fill:MovieClip;
		private var border:MovieClip;
		
		private var fillAmt:Number;
		private var fillColor:uint;
		
		private var barWidth:int;
		private var barHeight:int;
		
		/**
		 * Creates a rectangular fill bar
		 * @param	w Width of the bar
		 * @param	h Height of the bar
		 * @param	fillCol Color of the filled part of the bar
		 * @param	backgroundCol Color of the unfilled part of the bar
		 */
		public function FillBar(w:int, h:int, fillCol:uint = 0xFF0000, backgroundCol:uint = 0x00FFFF) 
		{
			//create the three clips
			background = new MovieClip();
			fill = new MovieClip();
			border = new MovieClip();
			
			//add them
			addChild(background);
			addChild(fill);
			addChild(border);
			
			//set persistent vars
			barWidth = w;
			barHeight = h;
			
			//draw the background
			var g:Graphics = background.graphics;
			g.lineStyle(1);
			g.moveTo(0, 0);
			g.beginFill(backgroundCol);
			g.lineTo(barWidth, 0);
			g.lineTo(barWidth, barHeight);
			g.lineTo(0, barHeight);
			g.lineTo(0, 0);
			g.endFill();
			
			//draw the border
			g = border.graphics;
			g.lineStyle(1);
			g.moveTo(0, 0);
			g.lineTo(barWidth, 0);
			g.lineTo(barWidth, barHeight);
			g.lineTo(0, barHeight);
			g.lineTo(0, 0);
			
			//start unfilled
			fillAmt = 0;
			
			//set the fill color
			fillColor = fillCol;
		}
		
		/**
		 * Get the amount the bar is filled
		 * @return the amount filled, as a decimal (example: 50% filled returns 0.5)
		 */
		public function getFill():Number
		{
			return (fillAmt);
		}

		/**
		 * Set and redraws the fill accordingly
		 * @param	amt the amount filled, as a decimal (example: 0.5 will fill it half). Less than 0 will set it to 0, and greater than 1 will set it to 1.
		 */
		public function setFill(amt:Number):void
		{
			if (amt < 0) { amt = 0; }
			else if (amt > 1) { amt = 1; }
			
			fillAmt = amt;
			
			var g:Graphics = fill.graphics;
			g.lineStyle(0);
			g.moveTo(0, 0);
			g.beginFill(fillColor);
			g.lineTo(barWidth*amt, 0);
			g.lineTo(barWidth*amt, barHeight);
			g.lineTo(0, barHeight);
			g.lineTo(0, 0);
			g.endFill();
		}
	}

}